﻿using BaseUI;
using BLL;
using System;
using System.Linq;
using System.Text.RegularExpressions;

namespace Thksoft.Module.BillManagement
{
    public partial class TradeBillDetailed : BasePage
    {
        private YX_TradeBillBLL bbbll = new YX_TradeBillBLL();
        private Sys_BankBLL sban = new Sys_BankBLL();
        private Sys_CompanyBLL scom = new Sys_CompanyBLL();
        private Sys_UserBLL user = new Sys_UserBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var key = Request.QueryString["ID"];
                if (key == "")
                {
                    MessageBox.Show(this, "参数ID传递错误");
                    return;
                }
                Key = Convert.ToDecimal(key);
                BindData();
            }
        }

        public decimal Key
        {
            get
            {
                var text = FORM_KEY.Value;
                if (text == "")
                {
                    return -1;
                }
                return Convert.ToDecimal(text);
            }
            set
            {
                FORM_KEY.Value = value.ToString();
            }
        }

        protected void BindData()
        {
            string id = Key.ToString();
            var data = bbbll.SelectById(id);
            var money = Convert.ToDecimal(data.N_TRADEBILL_INVOICEVALUE.ToString());

            var DRAWER = user.Find(data.N_TRADEBILL_DRAWERID).USER_NAME.ToString();
            var PAYERNAME = data.C_TRADEBILL_PAYERNAME.ToString();
            if (DRAWER == PAYERNAME)
            {
                lblName.Text = "商业";
            }
            else
            {
                lblName.Text = "银行";
            }

            lblDateNow.Text = DateTime.Now.ToString();
            Label10.Text = data.D_TRADEBILL_ENDDATE.ToString();
            lblDateStart.Text = data.D_TRADEBILL_STARTDATE.ToString();
            lblDateEnd.Text = data.D_TRADEBILL_ENDDATE.ToString();
            lblCODE.Text = data.C_TRADEBILL_CODE.ToString();
            lblDRAWERACCOUNT.Text = data.C_TRADEBILL_DRAWERACCOUNT.ToString();
            lblPAYEEACCOUNT.Text = data.C_TRADEBILL_PAYEEACCOUNT.ToString();
            lblINVOICEVALUE.Text = ConvertToChinese(money) + "(￥" + FormatValue(money) + ")";
            lblREMARK.Text = data.C_TRADEBILL_REMARK.ToString();

            lblDRAWERA_CMY.Text = scom.Find(data.N_TRADEBILL_DRAWERCMYID).CMY_NAME.ToString();
            lblDRAWERABank.Text = sban.Find(data.N_TRADEBILL_DRAWERBANKID).C_NAME.ToString();
            lblPAYEE_CMY.Text = data.C_TRADEBILL_PAYEECMYNAME.ToString();
            lblPAYEEBank.Text = data.C_TRADEBILL_PAYEEBANKNAME.ToString();
            if (data.N_TRADEBILL_IFOFFER == 1)
            {
                lblIFOFFER.Text = "承兑汇票能转让";
            }
            else
            {
                lblIFOFFER.Text = "承兑汇票不能转让";
            }

            lblDRAWERAName.Text = DRAWER;
            Label1.Text = PAYERNAME;
            lblPAYEENAME.Text = data.C_TRADEBILL_PAYEENAME.ToString();
            lblUserName.Text = CurrentUserName;
        }

        /// <summary>
        /// 分割
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public string FormatValue(decimal value)
        {
            string tempStr = value.ToString();
            Match m = Regex.Match(tempStr, @"(\d+)(\.\d+)?");
            string before_str = m.Groups[1].Value;
            string after_str = m.Groups[2].Value;
            before_str = string.Join("", before_str.Reverse());
            before_str = Regex.Replace(before_str, @"(?<=\G\d{3})(?=(?!$).)", ",");
            before_str = string.Join("", before_str.Reverse());
            return before_str + after_str;
        }

        /// <summary>
        /// 转换为大写
        /// </summary>
        /// <param name="x"></param>
        /// <returns></returns>
        private static string ConvertToChinese(decimal x)
        {
            string s = x.ToString("#L#E#D#C#K#E#D#C#J#E#D#C#I#E#D#C#H#E#D#C#G#E#D#C#F#E#D#C#.0B0A");
            string d = Regex.Replace(s, @"((?<=-|^)[^1-9]*)|((?'z'0)[0A-E]*((?=[1-9])|(?'-z'(?=[F-L\.]|$))))|((?'b'[F-L])(?'z'0)[0A-L]*((?=[1-9])|(?'-z'(?=[\.]|$))))", "${b}${z}");
            return Regex.Replace(d, ".", m => "负圆空零壹贰叁肆伍陆柒捌玖空空空空空空空分角拾佰仟万亿兆京垓秭穰"[m.Value[0] - '-'].ToString());
        }

        /// <summary>
        /// 打印
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnPrint_Click(object sender, EventArgs e)
        {
            var key = Guid.NewGuid().ToString();
            Session[key] = FORM_PRINT_VALUE.Value;//Server.HtmlEncode(FORM_PRINT_VALUE.Value);//this.tablemain.InnerHtml;
            MessageBox.ResponseScript(Page, "top.MessageBox.Print({Value:\"ID=HTML&VALUE=" + key + "\",Area:[\"800px\",\"500px\"],Scrollbar:true})");
        }
    }
}